package com.buddy.qreport.report.dao;

import com.buddy.qreport.common.mybatis.entity.Page;
import com.buddy.qreport.report.entity.ReportAlbumEntity;
import com.buddy.qreport.report.entity.ReportEntity;
import com.buddy.qreport.report.entity.ReportRelationEntity;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.StatementType;

import java.util.List;

@Mapper
public interface IReportMapper {


    @SelectKey(statementType = StatementType.PREPARED, resultType = String.class, keyProperty = "reportId", statement = "select replace(uuid(),'-','') from dual ", before = true)
    @Insert({
            "insert into t_report(report_id,report_name,report_desc,report_query,report_template,report_styleconfig,report_createtime)",
            "values",
            "(#{reportId},#{reportName},#{reportDesc},#{reportQuery},#{reportTemplate},#{reportStyleconfig},(UNIX_TIMESTAMP(now())*1000) )"

    })
    void insert(ReportEntity entity);


    @Insert({"<script>",

            "insert into t_report_relation(relation_id,relation_dimension,relation_column,relation_report)",
            "values",
            "<foreach collection =\"list\" item=\"item\" index= \"index\" separator =\",\">",

            "((select replace(uuid(),'-','')) ,#{item.relationDimension},#{item.relationColumn},#{reportId})",

            "</foreach>",

            "</script>"
    })
    void insertRelations(@Param("list") List<ReportRelationEntity> entities,@Param("reportId") String reportId);


    @Select("select * from t_report where report_id=#{id}")
    ReportEntity selectOne(String id);

    @Select("select * from t_report")
    List<ReportEntity> selectReport(Page page);


    @Delete("deleteReport from t_report where report_id = #{id}")
    void deleteReport(String reportId);


    @Select("select * from t_report_relation where relation_report=#{id}")
    List<ReportRelationEntity> selectRelations(String reportId);


    @Select("select * from t_report_album order by album_createtime desc")
    List<ReportAlbumEntity> selectReportAlbumList(Page page);

    @SelectKey(statementType = StatementType.PREPARED, resultType = String.class, keyProperty = "albumId", statement = "select replace(uuid(),'-','') from dual ", before = true)
    @Insert("insert into t_report_album(album_id,album_config,album_name,album_desc,album_createtime) values(#{albumId},#{albumConfig},#{albumName},#{albumDesc},(unix_timestamp(now()) * 1000))")
    void saveReportAlbum(ReportAlbumEntity entity);

    @Update("update t_report_album set album_config = #{albumConfig},album_name = #{albumName},album_desc=#{albumDesc} where album_id=#{albumId}")
    void updateReportAlbum(ReportAlbumEntity entity);

    @Select("select * from t_report_album where album_id = #{id}")
    ReportAlbumEntity selectOneReportAlbum(String id);


    @Delete("delete from t_report_album where album_id = #{id}")
    void deleteAlbum(String albumId);
}
